package com.atguigu.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.realtime.app.BaseAppV1;
import com.atguigu.realtime.common.Constant;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/2/12 10:22
 */
public class DwmUserJumpDetailApp extends BaseAppV1 {
    public static void main(String[] args) {
        new DwmUserJumpDetailApp().init(3002, 1, "DwmUserJumpDetailApp", "DwmUserJumpDetailApp", Constant.TOPIC_DWD_PAGE);
    }
    
    @Override
    protected void run(StreamExecutionEnvironment env,
                       DataStreamSource<String> stream) {
        stream = env.fromElements(
            "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000} ",
            "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":11000} ",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000}",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                "\"home\"},\"ts\":16000} ",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                "\"detail\"},\"ts\":50000} "
        );
        
        KeyedStream<JSONObject, String> keyedStream = stream
            .map(JSON::parseObject)
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner((obj, ts) -> obj.getLong("ts"))
            )
            .keyBy(obj -> obj.getJSONObject("common").getString("mid"));
        
        // 1. 定义模式
        /*
        {
          "common": {
            "ar": "370000",
            "uid": "40",
            "os": "Android 11.0",
            "ch": "oppo",
            "is_new": 0,
            "md": "Xiaomi 10 Pro ",
            "mid": "mid_47",
            "vc": "v2.1.134",
            "ba": "Xiaomi"
          },
          "page": {
            "page_id": "payment",
            "item": "2,9",
            "during_time": 9760,
            "item_type": "sku_ids",
            "last_page_id": "home"
          },
          "ts": 1644629262000
        }
         */
        Pattern<JSONObject, JSONObject> pattern = Pattern
            .<JSONObject>begin("entry")
            .where(new SimpleCondition<JSONObject>() {
                @Override
                public boolean filter(JSONObject value) throws Exception {
                    String lastPageId = value.getJSONObject("page").getString("last_page_id");
                    return lastPageId == null || lastPageId.length() == 0;
                }
            })
            .next("normal")
            .where(new SimpleCondition<JSONObject>() {
                @Override
                public boolean filter(JSONObject value) throws Exception {
                    String lastPageId = value.getJSONObject("page").getString("last_page_id");
                    return lastPageId != null && lastPageId.length() > 0;
                }
            })
            .within(Time.seconds(5));
        
        // 2. 把模式作用在流上, 得到一个模式流
        PatternStream<JSONObject> ps = CEP.pattern(keyedStream, pattern);
        // 3. 从模式流获取到需要的数据: 1. 匹配到数据   2. 超时数据
        
        SingleOutputStreamOperator<JSONObject> normal = ps.select(
            new OutputTag<JSONObject>("uj") {},
            // 超时的数据, 就是跳出数据
            new PatternTimeoutFunction<JSONObject, JSONObject>() {
                @Override
                public JSONObject timeout(Map<String, List<JSONObject>> map, long l) throws Exception {
                    return map.get("entry").get(0);
                }
            },
            // 匹配上的数据, 就是非跳出, 不用处理
            new PatternSelectFunction<JSONObject, JSONObject>() {
                @Override
                public JSONObject select(Map<String, List<JSONObject>> map) throws Exception {
                    return null;
                }
            }
        
        );
        
        normal.getSideOutput(new OutputTag<JSONObject>("uj") {}).print();
    }
}
/*
跳出率:
    跳出的次数/进入的总次数
    
    跳出率高:
        1. 网站质量不行
        2. 通过广告吸引来的用户不是目标用户
        
        
   分母:
     进入的次数
        入口访问记录
            last_page_id是null
            
   分子:
     跳出的页面, 统计有多少跳出页面
     dwm: 找到只访问入口, 没有再继续访问其他的页面的入口页面, 跳出现明细
     
     cep实现:
        第一个是入口, 过了一段时间没有新的页面

跳出:
 


 */